﻿package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.model.Product;
	import com.taobao.top.model.ProductImg;
	import com.taobao.top.model.ProductPropImg;
	import com.taobao.top.resultmodel.ProductSearchResult;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	public class TOPProduct extends EventDispatcher
	{
		public function TOPProduct()
		{
		}
		
		public function getProductByProductId(fields:Array, product_id:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.product.get"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),new TopRequestParam("product_id",product_id)];
			topURLLdr.addEventListener(Event.COMPLETE,productGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function getProductByCidAndProp(fields:Array, cid:String, props:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.product.get"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),new TopRequestParam("cid",cid),new TopRequestParam("props",props)];
			topURLLdr.addEventListener(Event.COMPLETE,productGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		
		public function searchProductByCidAndProp(fileds:Array,fields:Array, cid:String, props:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.product.search"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),new TopRequestParam("cid",cid),new TopRequestParam("props",props)];
			topURLLdr.addEventListener(Event.COMPLETE,productsSearchHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function searchProductByKeyword(fields:Array,keyword:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.product.search"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),new TopRequestParam("keyword",keyword)];
			topURLLdr.addEventListener(Event.COMPLETE,productsSearchHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		
		public function addProductByProps(cid:String,outer_id:String,props:String,name:String,price:String,image:String,binds:String = "",sale_props:String="",desc:String=""):void
		{
			
		}
		
		public function addProductByCustomer_props (cid:String,outer_id:String,customer_props:String,name:String,price:String,image:String,binds:String = "",sale_props:String="",desc:String=""):void
		{
			
		}
		
		public function uploadImg(product_id:String,image:ByteArray,pic_id:String="",position:int=0,is_major:Boolean=false):void
		{
			
		}
		
		public function uploadPropImg(product_id:String,image:ByteArray,pic_id:String = "",position:int = 0,is_major:Boolean = false):void
		{
			
		}
		
		public function updateProduct():void
		{
			
		}
		
		public function deleteImg():void
		{
			
		}
		
		public function deletePropImg():void
		{
			
		}
		
		
		private function productGetHandler(event:Event):void
		{
			trace(event.target.data);
			switch(event.type)
			{
				case Event.COMPLETE:
					var product:Product = productSearchJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function productSearchJsonParser(jsonStr:String):Product
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objectToProduct((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function objectToProduct(productObj:Object):Product
		{
			var product:Product = new Product();
			product.binds = productObj.binds;
			product.binds_str = productObj.binds_str;
			product.cat_name = productObj.cat_name;
			product.cid =productObj.cid;
			product.created  = TOPUtil.getCustomDate( productObj.created);
			product.desc = productObj.desc;
			product.modified = TOPUtil.getCustomDate(productObj.modified);
			product.name = productObj.name;
			product.outer_id = productObj.outer_id;
			product.pic_path = productObj.pic_path;
			product.price = productObj.price;
			product.product_id = productObj.product_id;
			for each(var pdImgObj:Object in productObj.productImg)
			{
				var pdImg:ProductImg = new ProductImg();
				pdImg.pic_id = pdImgObj.pid_id;
				pdImg.position = pdImgObj.position;
				pdImg.product_id = pdImgObj.product_id;
				pdImg.url = pdImgObj.url;
				product.productImg.push(pdImg);
			}
			for each(var propImgObj:Object in productObj.productImg)
			{
				var propImg:ProductPropImg = new ProductPropImg();
				propImg.pic_id = propImgObj.pid_id;
				propImg.position = propImgObj.position;
				propImg.product_id = propImgObj.product_id;
				propImg.props = propImgObj.props;
				propImg.url = propImgObj.url;
				product.productPropImg.push(propImg);
			}
			product.props = productObj.props;
			product.props_str = productObj.props_str;
			product.sale_props = productObj.sale_props;
			product.sale_props_str = productObj.sale_props_str;
			product.tsc = productObj.tsc;
			return product;
		}
		
		
		
		private function productsSearchHandler(event:Event):void
		{
			trace(event.target.data);
			switch(event.type)
			{
				case Event.COMPLETE:
					var product:Product = productSearchJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function productsSearchJsonParser(jsonStr:String):ProductSearchResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return productsSearchParser((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function productsSearchParser(resultObj:Object):ProductSearchResult
		{
			var productSearchResult:ProductSearchResult = new ProductSearchResult();
			productSearchResult.totalResults = resultObj.resultObj;
			for each(var pdObj:Object in resultObj.products)
			{
				productSearchResult.products.push(objectToProduct(pdObj));
			}
			return productSearchResult;
		}
	}
}